package com.aforce.entities;

import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Group;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import com.aforce.appearances.BoxAppearance;
import com.aforce.behaviors.CollisionDetector;
import com.aforce.models.Spike;

public class Mine extends AbstractEntity
{
	TransformGroup ogl_group = new TransformGroup();
	
	public Mine(double x_position, double y_position, double z_position)
	{
		Vector3d pos = new Vector3d(x_position, y_position, z_position);
		
		ogl_group.addChild(new MineSpike(-1, -1, -1).getTransformGroup());
		ogl_group.addChild(new MineSpike(-1, -1, 1).getTransformGroup());
		ogl_group.addChild(new MineSpike(-1, 1, -1).getTransformGroup());
		ogl_group.addChild(new MineSpike(-1, 1, 1).getTransformGroup());
		ogl_group.addChild(new MineSpike(1, -1, -1).getTransformGroup());
		ogl_group.addChild(new MineSpike(1, -1, 1).getTransformGroup());
		ogl_group.addChild(new MineSpike(1, 1, -1).getTransformGroup());
		ogl_group.addChild(new MineSpike(1, 1, 1).getTransformGroup());
		
		Transform3D trans = new Transform3D();
		ogl_group.getTransform(trans);
		
		trans.set(pos);
		
		ogl_group.setTransform(trans);
	}

	@Override
	public Group getTransformGroup()
	{
		return ogl_group;
	}
}


/**
 * A spike is part of a mine.  The spike's transform group should return one of the pins of the mine.
 * The spike geometry should be transformed such that the origin of the returned transform group is the center of the blunt side of the spike
 *
 */

class MineSpike extends AbstractEntity
{
	TransformGroup ogl_group = new TransformGroup();
	Spike spike = null;
	private static BoxAppearance boxAppearances = new BoxAppearance();
	
	/** x, y, and z coordinates are the position of the tip of the spike relative to the returned transform group's position **/

	public MineSpike(double x_position, double y_position, double z_position)
	{
		ogl_group = new TransformGroup();
		
		spike = new Spike(x_position, y_position, z_position);
		
		double scale = 0.2;
		
		// Create a transform group node to scale and position the object.
		Transform3D t = new Transform3D();
		t.set(scale);
		
		ogl_group.setTransform(t);
		
		
		Appearance shapeAppearance = boxAppearances.createAppearance(9);
		spike.setAppearance(shapeAppearance);
		
		
		CollisionDetector cd = new CollisionDetector(spike);
		BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
		cd.setSchedulingBounds(bounds);
		
		
		ogl_group.addChild(spike);
		
		ogl_group.addChild(cd);
		
		// TODO: Implement spike geometry
	}

	@Override
	public Group getTransformGroup()
	{
		return ogl_group;
	}
}
